//
//  StoreQuickReplyView.m
//  BaseProject
//
//  Created by mac on 2020/12/24.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "StoreQuickReplyView.h"
#import "TIIMQuickReplyCardModel.h"
@interface StoreQuickReplyView ()
@property(nonatomic,strong)UIControl *bgControl;
@property(nonatomic,assign)CGFloat animationDuration;
@property(nonatomic,weak)UIButton *replyButton;
@property(nonatomic,weak)UIViewController *parentVC;
@property(nonatomic,strong)NSMutableArray<UIView *> *itemsArray;
@end

@implementation StoreQuickReplyView
- (instancetype)initWithFrame:(CGRect)frame withParent:(nonnull UIViewController *)parentVC withReplyButton:(nonnull UIButton *)replyButon
{
    if (self = [super initWithFrame:frame]) {
        self.parentVC = parentVC;
        self.replyButton = replyButon;
        [self createProperty];
        [self createUI];
    }
    return self;
}
- (void)dealloc
{
    NSLog(@"StoreQuickReplyView ---dealloc");
}
- (void)createProperty
{
    self.animationDuration = 0.2;
}
- (void)createUI
{
    [self.bgControl addSubview:self.replyButton];
    [self.bgControl addSubview:self];
    self.frame = CGRectMake(WIDTH - 181*ADAPTER_WIDTH, 0, 165*ADAPTER_WIDTH, 164*ADAPTER_WIDTH);
    self.bottom = self.replyButton.top - 8*ADAPTER_WIDTH;
    self.alpha = 0;
    [self startAnimation];
}
- (void)setQuickReplyModel:(TIIMQuickReplyCardModel *)quickReplyModel
{
    _quickReplyModel = quickReplyModel;
    self.height = quickReplyModel.data.count * 34*ADAPTER_WIDTH - 6*ADAPTER_WIDTH;
    self.bottom = self.replyButton.top - 8*ADAPTER_WIDTH;
    
    CGFloat orinY = 0;
    for (NSInteger index = 0; index < quickReplyModel.data.count; index ++) {
        UIControl *control = [[UIControl alloc]initWithFrame:CGRectMake(0, orinY, self.width, 28*ADAPTER_WIDTH)];
        [self addSubview:control];
        control.backgroundColor = WhiteColor;
        control.tag = index;
        [control addTarget:self action:@selector(clickItemControl:) forControlEvents:UIControlEventTouchUpInside];
        control.layer.cornerRadius = 14*ADAPTER_WIDTH;
        control.layer.masksToBounds = YES;
        orinY = control.bottom + 6*ADAPTER_WIDTH;
        
        UILabel *lab = [[UILabel alloc]init];
        [control addSubview:lab];
        lab.frame = CGRectMake(10*ADAPTER_WIDTH, 0, 120*ADAPTER_WIDTH, control.height);
        lab.textColor = kColor102;
        lab.font = kFontRegular12;
        lab.text = quickReplyModel.data[index].content;
        
        UIImageView *iv = [[UIImageView alloc]init];
        [control addSubview:iv];
        iv.image = [UIImage imageNamed:@"QuickReply_arrow"];
        iv.frame = CGRectMake(control.width - 22*ADAPTER_WIDTH, 6*ADAPTER_WIDTH, 16*ADAPTER_WIDTH, 16*ADAPTER_WIDTH);
    }
}
#pragma mark - click
- (void)dismiss
{
    [self.parentVC.view addSubview:self.replyButton];
    self.replyButton.selected = NO;
    self.replyButton.backgroundColor = rgba(76, 198, 253, 1);
    [self.replyButton setImage:[UIImage imageNamed:@"QuickReply_up"] forState:UIControlStateNormal];
    [self endAnimation];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((self.animationDuration + 0.01) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj removeFromSuperview];
        }];
        [self.bgControl.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj removeFromSuperview];
        }];
        [self.bgControl removeFromSuperview];
    });
}
- (void)clickItemControl:(UIControl *)sender
{
    !self.callBackContentBlock ?: self.callBackContentBlock (self.quickReplyModel.data[sender.tag].content);
    [self dismiss];
}
#pragma mark - 动画
- (void)startAnimation
{
    WK(weakSelf)
    [UIView animateWithDuration:self.animationDuration animations:^{
        weakSelf.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}
- (void)endAnimation
{
    WK(weakSelf)
    [UIView animateWithDuration:self.animationDuration animations:^{
        weakSelf.alpha = 0;
    } completion:^(BOOL finished) {

    }];
}
#pragma mark - lazy懒加载
- (UIControl *)bgControl
{
    if (!_bgControl) {
        UIControl *view = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        [self.parentVC.view addSubview:view];
        view.backgroundColor = rgba(0, 0, 0, 0.2);
        [view addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        _bgControl = view;
    }
    return _bgControl;
}
- (NSMutableArray<UIView *> *)itemsArray
{
    if (!_itemsArray) {
        _itemsArray = [NSMutableArray array];
    }
    return _itemsArray;
}
@end
